//
//  DeviceInfoViewController.m
//  SenMo
//
//  Created by ZengYuan on 15/12/17.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "DeviceInfoViewController.h"
#import "DeviceHeadCell.h"
#import "DeviceHandle.h"
#import "SwitchSetCell.h"
#import "ShowSetCell.h"
#import "ShareDeviceManageViewController.h"
#import "RequestViewController.h"
#import "ShowQRCodeViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "InputTextViewController.h"
#import "InputDeviceAdmViewController.h"


@interface DeviceInfoViewController ()
{
    UITableView *tableView;
    NSMutableArray *dataArray;
}
@end

@implementation DeviceInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - NAVBARHEIGHT_STATUSBAR);
    
    tableView = [[UITableView alloc]initWithFrame:rect
                                            style:UITableViewStylePlain];
    tableView.backgroundColor = MAIN_DARKCOLOR;
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    dataArray=[NSMutableArray arrayWithObjects:
               [NSArray arrayWithObjects:@"header",nil],
               [NSArray arrayWithObjects:@"名称",nil],
               [NSArray arrayWithObjects:@"二维码",@"微信分享",@"版本号",nil],
               [NSArray arrayWithObjects:@"分享视频",@"设置账号密码",@"设备下线提醒",nil],
               [NSArray arrayWithObjects:@"删除设备",nil],

               nil];

    self.titleLabel.text = @"设备";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = dataArray[section];
    
    if (section==3 && self.sdevice.value == 2) {
        return 1;
    }
    
    return [array count];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return (section==0 ||section == 2)?0:20;
}



- (UIView *)tableView:(UITableView *)_tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        static NSString *CellDHC = @"CellDHC";
        DeviceHeadCell *cell=[_tableView dequeueReusableCellWithIdentifier:CellDHC];
        if (!cell) {
            cell=[[[NSBundle mainBundle] loadNibNamed:@"DeviceHeadCell" owner:self options:nil] objectAtIndex:0];
            [cell addBottomLine];
        }
        
        //        UserUnit *jkunit=[UserUnit getActiveUser];
//        NSString *url = nil;
        
        
        cell.nameLabel.text = self.sdevice.device_name;
        //        jkunit.userName;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.rowType = 3;
//        cell.rowType = [self rowIndex:indexPath];
        
        return cell;
        
    }

    
    //设备下线提醒
    if (indexPath.section == 3 && indexPath.row == [self tableView:tableView numberOfRowsInSection:3]-1) {

        SettingCell *cell;

        static NSString *CellSS = @"CellSS";
        cell=[_tableView dequeueReusableCellWithIdentifier:CellSS];
        if (!cell) {
            cell=[[SwitchSetCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellSS];
        }
        
        ((SwitchSetCell *)cell).toggle.on=YES;
        cell.textLabel.text=@"视频下线提醒";
        return cell;
        
    }
    
    else{
        
        ShowSetCell *cell;

        static NSString *CellSC = @"CellSC";
        cell=[_tableView dequeueReusableCellWithIdentifier:CellSC];
        if (!cell) {
            cell=[[ShowSetCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellSC];
        }
        
        cell.backgroundColor=[UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        NSArray *array = dataArray[indexPath.section];
        cell.rowType =3;
        cell.textLabel.text=[array objectAtIndex:indexPath.row];

        
        if(indexPath.section == 1){
            cell.contentLabel.text = self.sdevice.device_name;
        }
        else if (indexPath.section == 2) {
            if (indexPath.row == 0) {
                cell.showImagView.image=[UIImage imageNamed:@"erweima"];
            } else if (indexPath.row == 1) {
                cell.showImagView.image=[UIImage imageNamed:@"fenxiang"];
            }


        }

        
        return cell;
        
        
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0 && indexPath.row == 0){
        return 93;
    }
    
    return 60;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 4) {
        [self unbindDevice];
    }
    if(indexPath.section == 1){
    
        InputTextViewController *itvc = [[InputTextViewController alloc]init];
        itvc.originalText = self.sdevice.device_name;
        itvc.navTitle = @"设备名";
        itvc.delegate = self;
        [self.navigationController pushViewController:itvc animated:YES];
    }
    if(indexPath.section == 2){
        if (indexPath.row == 0) {
            //二维码
       
            ShowQRCodeViewController *sqvc = [[ShowQRCodeViewController alloc]init];
            sqvc.qrcode = self.sdevice.device_id;
            [self.navigationController pushViewController:sqvc animated:YES];
            
        }else  if (indexPath.row == 1) {
            //微信分享
//            MBALERT(@"unavailable now");
            
            [self shareWeixin];
            
        }else  if (indexPath.row == 2) {
            //版本号
            
        }
    }else if(indexPath.section == 3){
        if(self.sdevice.value == 1 && indexPath.row == 0){

            //视频分享
            ShareDeviceManageViewController *sdmvc = [[ShareDeviceManageViewController alloc]init];
            sdmvc.device =self.sdevice;
            [self.navigationController pushViewController:sdmvc animated:YES];
        }
        else if(self.sdevice.value == 1 && indexPath.row == 1){

            //设置账号密码
            InputDeviceAdmViewController *evc = [InputDeviceAdmViewController fromSB:@"InputDeviceAdmViewController"];
            evc.device_id = self.sdevice.device_id;
            evc.device_name=self.sdevice.device_name;
            evc.action_target = 1;
            [self.navigationController pushViewController:evc animated:YES];
            
        }
        
        else{

        }
    }
    
    
    
    
}

-(void)DidInputNewText:(NSString *)text{
    self.sdevice.device_name = text;
    [tableView reloadData];
    
    [DeviceHandle deviceChangeName:text deviceId:self.sdevice.device_id success:^{
        MBALERT(@"修改成功");
    } failure:^(NSString *error) {
        MBALERT(@"修改失败");

    }];
    
}


-(void)unbindDevice{
    
    UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"确定要删除此设备吗" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", @"确定",nil];
    [alertview show];
    alertview.tag=300;
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 300 && buttonIndex==1) {
        
        [DeviceHandle deviceUnbind:self.sdevice.device_id success:^{
            MBALERT(@"删除成功");
            if (self.unbindBlock) {
                self.unbindBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(NSString *error) {
            MBALERT(@"删除失败");
        }];
        
    }
}

-(void)shareWeixin{
    
    NSString *tempTitle = @"我通过#智橙#分享了一个设备给您";
    NSString *content = [NSString stringWithFormat:@"设备号 %@", self.sdevice.device_id];
     //构造分享内容
    
    id<ISSContent> publishContent = [ShareSDK content:content
                                       defaultContent:content
                                                image:nil
                                                title:tempTitle
                                                  url:nil
                                          description:tempTitle
                                            mediaType:SSPublishContentMediaTypeText];
    
    
    //定制微信好友信息
    
    [publishContent addWeixinSessionUnitWithType:INHERIT_VALUE
     
                                         content:INHERIT_VALUE
                                           title:tempTitle
                                             url:INHERIT_VALUE
                                      thumbImage:nil                                           image:INHERIT_VALUE
                                    musicFileUrl:nil
                                         extInfo:nil
                                        fileData:nil
                                    emoticonData:nil];
    
    [publishContent addWeixinTimelineUnitWithType:INHERIT_VALUE content:INHERIT_VALUE title:INHERIT_VALUE url:INHERIT_VALUE image:INHERIT_VALUE musicFileUrl:nil extInfo:nil fileData:nil emoticonData:nil];
    
    //弹出分享菜单
    
    [ShareSDK showShareActionSheet:nil
     
                         shareList:nil
                           content:publishContent
                     statusBarTips:YES
                       authOptions:nil
                      shareOptions:nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                
                                
                                
                                if (state == SSResponseStateSuccess)
                                    
                                {
                                    
                                    NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                    
                                }
                                
                                else if (state == SSResponseStateFail)
                                    
                                {
                                    
                                    
                                    MBALERT([error errorDescription]);
                                    
                                }
                                
                            }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
